function $(id) {
    return document.getElementById(id);
}

function empty(json) {
    var count = 0;
    for (var i in json) {
        count++
    }
    if (count > 0) {
        return false;
    }
    return true;
}

function showTable(table) {
    var from1 = $('from1');
    from1.innerHTML = table;
    var inputs = from1.getElementsByTagName("input");
    for (i = 0; i < inputs.length; i++) {
        inputs[i].onkeyup = callback;
    }
}

function getFromData() {
    var inputs = document.getElementsByTagName('input');
    var date = {};
    var jsonStr = window.localStorage.getItem('json');
    if (jsonStr) {
        date = JSON.parse(jsonStr);
    }
    for (k in inputs) {
        var value = inputs[k].value;
        var name = inputs[k].name;
        if (value) {
            date[name] = value;
        }
    }
    return date;
}

function date1(foramt, timestamp) {
    if (typeof foramt === 'undefined') {
        throw new Error('Invalid argument');
    }
    var date = timestamp ? new Date(timestamp * 1000) : new Date();
    var string = foramt
        .replace(/Y/g, date.getFullYear())
        .replace(/m/g, (function (d) {
            var value = d.getMonth() + 1;
            if (value < 10) {
                value = '0' + value;
            }
            return value;
        })(date))
        .replace(/d/g, (function (d) {
            var value = d.getDate();
            if (value < 10) {
                value = '0' + value;
            }
            return value;
        })(date))
        .replace(/H/g, (function (d) {
            var value = d.getHours();
            if (value < 10) {
                value = '0' + value;
            }
            return value;
        })(date))
        .replace(/i/g, (function (d) {
            var value = d.getMinutes();
            if (value < 10) {
                value = '0' + value;
            }
            return value;
        })(date))
        .replace(/s/g, (function (d) {
            var value = d.getSeconds();
            if (value < 10) {
                value = '0' + value;
            }
            return value;
        })(date))
        .replace(/w/g, (function (d) {
            var value = d.getDay();
            if (!value) {
                value = 7;
            }
            return value;
        })(date))
    ;
    return string;
}

function sxzy(e) {
    var event = e || window.event;  // 标准化事件对象
    switch (event.keyCode) {  // 获取当前按下键盘键的编码
        case 37 :  // 按下左箭头键，向左移动5个像素
            onleft(event);
            break;
        case 39 :  // 按下右箭头键，向右移动5个像素
            onright(event);
            break;
        case 38 :  // 按下上箭头键，向上移动5个像素
            onup(event)
            break;
        case 40 :  // 按下下箭头键，向下移动5个像素
            ondown(event)
            break;
    }
}
function onleft(event){
    var d = event.target;
    var path = d.parentNode.parentNode.lastElementChild.value;
    var pathArr = path.split('-');
    var i = pathArr[0];
    var j = pathArr[1];
    var rowDom =  d.parentNode.parentNode.lastElementChild.previousElementSibling.value;
    console.log(rowDom);
    var row = eval("(" + rowDom + ")");
    var name = event.target.name;
    var parse = parseName(name);
    var search = parse.type.indexOf('hour');
    console.log(search);
   if(search != -1){
       console.log(row.yestoday);
       console.log(row);
        if(row.yestoday!=""){
            var yestodayArr = row.yestoday.split('-');
            var typeArr =  parse.type.split('_');
            var targetname = 'date['+yestodayArr[0]+'-'+parseInt(yestodayArr[1])+']['+typeArr[0]+'_min]['+parseInt(yestodayArr[2])+']'
            document.getElementsByName(targetname)[0].focus();
        }
   }else if(parse.type.indexOf('min')!=-1){
      var dname =   event.target.name.replace('min','hour');
       document.getElementsByName(dname)[0].focus();
   }
}
function onright(event){
    var d = event.target;
    var path = d.parentNode.parentNode.lastElementChild.value;
    var pathArr = path.split('-');
    var i = pathArr[0];
    var j = pathArr[1];
    var rowDom =  d.parentNode.parentNode.lastElementChild.previousElementSibling.value;
    console.log(rowDom);
    var row = eval("(" + rowDom + ")");
    var name = event.target.name;
    var parse = parseName(name);
    var search = parse.type.indexOf('min');
    console.log(search);
    if(search != -1){
        console.log(row.next_date);
        console.log(row);
        if(row.next_date!=""){
            var yestodayArr = row.next_date.split('-');
            var typeArr =  parse.type.split('_');
            var targetname = 'date['+yestodayArr[0]+'-'+parseInt(yestodayArr[1])+']['+typeArr[0]+'_hour]['+parseInt(yestodayArr[2])+']'
            console.log(targetname);
            document.getElementsByName(targetname)[0].focus();
        }
    }else if(parse.type.indexOf('hour')!=-1){
        var dname =   event.target.name.replace('hour','min');
        document.getElementsByName(dname)[0].focus();
    }
}

function onup(event){
    var d = event.target;
    var path = d.parentNode.parentNode.lastElementChild.value;
    var pathArr = path.split('-');
    var i = pathArr[0];
    var j = pathArr[1];
    var rowDom =  d.parentNode.parentNode.lastElementChild.previousElementSibling.value;
    console.log(rowDom);
    var row = eval("(" + rowDom + ")");
    var name = event.target.name;
    var parse = parseName(name);
    var search = parse.type.indexOf('st');
    console.log(search);
    if(search != -1){
        console.log(row.up_day);
        console.log(row);
        if(row.up_day!=""){
            var yestodayArr = row.up_day.split('-');
            var typeArr =  parse.type.split('_');
            var targetname = 'date['+yestodayArr[0]+'-'+parseInt(yestodayArr[1])+'][nd_'+typeArr[1]+']['+parseInt(yestodayArr[2])+']'
            document.getElementsByName(targetname)[0].focus();
        }
    }else if(parse.type.indexOf('nd')!=-1){
        var dname =   event.target.name.replace('nd','st');
        document.getElementsByName(dname)[0].focus();
    }
}


function ondown(event){
    var d = event.target;
    var path = d.parentNode.parentNode.lastElementChild.value;
    var pathArr = path.split('-');
    var i = pathArr[0];
    var j = pathArr[1];
    var rowDom =  d.parentNode.parentNode.lastElementChild.previousElementSibling.value;
    console.log(rowDom);
    var row = eval("(" + rowDom + ")");
    var name = event.target.name;
    var parse = parseName(name);
    var search = parse.type.indexOf('nd');
    console.log(search);
    if(search != -1){
        console.log(row.down_day);
        console.log(row);
        if(row.down_day!=""){
            var yestodayArr = row.down_day.split('-');
            var typeArr =  parse.type.split('_');
            var targetname = 'date['+yestodayArr[0]+'-'+parseInt(yestodayArr[1])+'][st_'+typeArr[1]+']['+parseInt(yestodayArr[2])+']'
            document.getElementsByName(targetname)[0].focus();
        }
    }else if(parse.type.indexOf('st')!=-1){
        var dname =   event.target.name.replace('st','nd');
        document.getElementsByName(dname)[0].focus();
    }
}

 function callback(e) {
    if (this.value.length >= 2) {
        let name = e.target.name
        var thisRegex = /date\[(\S+)\]\[(\S+)\]\[(\S+)\]/i;
        var matches = name.match(thisRegex);
        var ym = matches[1];
        var type = matches[2];
        if (type == 'st_min' || type == 'nd_min') {
            var d = matches[3];
            var str = ym + "-" + d;
            var time = parseInt(Date.parse(str).valueOf() / 1000);
            var tomorrowtime = time;
            if (type == 'nd_min') {
                tomorrowtime = time + 86400;
            }
            var date = date1('Y-m-d', tomorrowtime);
            var dateArr = date.split('-');
            var nextym = dateArr[0] + "-" + parseInt(dateArr[1]);
            var nextType = 'nd_hour';
            if (type == 'nd_min') {
                nextType = 'st_hour';
            }
            var nextName = 'date[' + nextym + '][' + nextType + '][' + parseInt(dateArr[2]) + ']';
            console.log(nextName);
            document.getElementsByName(nextName)[0].focus();

        } else {
            e.target.nextElementSibling.focus();
        }

    }
    sxzy(e);
}









function parseName(name){
    var thisRegex = /date\[(\S+)\]\[(\S+)\]\[(\S+)\]/i;
    var matches = name.match(thisRegex);
    var ym = matches[1];
    var type = matches[2];
    return {
        ym:ym,
        type:type
    }
}

window.onload = function () {
    $("btn").onclick = function () {
        var from1 = document.getElementById('from1');
        from1.submit();
        var date = getFromData();
        if (!empty(date)) {
            var json = JSON.stringify(date);
            window.localStorage.setItem('json', json);
        }
    }
    $('btn2').onclick = function () {
        var jsonStr = window.localStorage.getItem('json');
        if (jsonStr) {
            let json = JSON.parse(jsonStr);
            for (k in json) {
                var dom = document.getElementsByName(k)[0];
                if (dom) {
                    dom.value = json[k];
                }
            }
        }
    }
    $('btn3').onclick = function () {
        var inputs = document.getElementsByTagName('input');
        for (var i in inputs) {
            inputs[i].value = '';
        }
    }
    $("select1").onchange = function () {
        var val = this.value;
        var date = val.split('-');
        $('h1').innerHTML = date[0] + "年" + date[1] + "月";
        var from2 = $('from2');
        from2.submit();
    }
    var inputs = document.getElementsByTagName("input");
    for (i = 0; i < inputs.length; i++) {
        inputs[i].onkeyup = callback;
    }

}
